package com.multysite.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.multysite.model.NewsModel;
import com.multysite.util.Setting;

@SuppressWarnings("serial")
public class ListNewsServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(ListNewsServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			System.out.println("Ok add");
			resp.setCharacterEncoding("utf-8");
			if (!NamespaceManager.get().equals(Setting.getGeneralNamespace())) {
				int page = 1;
				try {
					page = Integer.parseInt((String) req.getParameter("page"));
				} catch (Exception e) {
					page = 1;
				}
				NewsModel model = new NewsModel();
				model.setPage(page);
				model.setLimit(10);
				model.prepareList();
				req.setAttribute("model", model);
				req.getRequestDispatcher("/admin/pages/news-list.jsp").forward(
						req, resp);
			} else {
				resp.getWriter().println("Invalid Url");
			}
		} catch (ServletException e) {
			e.printStackTrace();
			log.warning(e.toString());
			resp.sendError(4000,
					"Website is down for maintenance. We are sorry for the inconvenience !");
		}
	}

}
